﻿using System;
using System.Collections.Generic;
using System.Linq;
using TradeMeSDK;

namespace TradeMo
{
    public class CriteriaBoatsViewModel : BaseViewModel
    {
        private SearchBoatsCriteria _criteria;
        public SearchBoatsCriteria Criteria
        {
            get
            {
                if (_criteria == null)
                {
                    _criteria = new SearchBoatsCriteria();
                    UpdateFields();
                }
                return _criteria;
            }
            set
            {
                _criteria = value;
                UpdateFields();
            }
        }


        ////////////////////////////////////////////////////////////////////////////
        
        private Locality _selectedRegion;
        public Locality SelectedRegion
        {
            get { return _selectedRegion; }
            set
            {
                _selectedRegion = value;
                NotifyPropertyChanged("RegionName");
                Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
            }
        }

        public string RegionName
        {
            get
            {
                return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
            }
        }

        public List<SelectorItem<Locality>> Regions
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<Locality>>();
                list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
                list.AddRange(
                    App.TradeMe.Catalog.Areas.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
                return list.ToList();
            }
        }


        ////////////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedSortBy;
        public SelectorItem<string> SelectedSortBy
        {
            get
            {
                return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string> { Title = "Default" });
            }
            set
            {
                _selectedSortBy = value;
                NotifyPropertyChanged("SortByName");
                Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
            }
        }
        public string SortByName
        {
            get { return SelectedSortBy.Title; }
            set { SelectedSortBy.Title = value; }
        }

        public string SortByItem
        {
            get { return Criteria.SortOrder; }
            set { Criteria.SortOrder = value; }
        }

        public List<SelectorItem<string>> SortByItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Title", Item = "TitleAsc"},
                        new SelectorItem<string> {Title = "Closing Soon", Item = "ExpiryAsc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                        new SelectorItem<string> {Title = "Lowest Price", Item = "PriceAsc"},
                        new SelectorItem<string> {Title = "Highest Price", Item = "PriceDesc"},
                        new SelectorItem<string> {Title = "Most Bids", Item = "BidsMost"},
                        new SelectorItem<string> {Title = "Lowest Buy Now", Item = "BuyNowAsc"},
                        new SelectorItem<string> {Title = "Highest Buy Now", Item = "BuyNowDes"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedType;
        public SelectorItem<string> SelectedType
        {
            get
            {
                return _selectedType ?? (_selectedType = new SelectorItem<string> { Title = "All" });
            }
            set
            {
                _selectedType = value;
                NotifyPropertyChanged("TypeName");
                Criteria.Type = _selectedType != null ? _selectedType.Item : null;
            }
        }
        public string TypeName
        {
            get { return SelectedType.Title; }
            set { SelectedType.Title = value; }
        }
        public string TypeItem
        {
            get { return Criteria.Type; }
            set { Criteria.Type = value; }
        }

        public List<SelectorItem<string>> TypeItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "All", Item = "Default"},
                        new SelectorItem<string> {Title = "Fibreglass", Item = "Fibreglass"},
                        new SelectorItem<string> {Title = "Aluminium", Item = "Aluminium"},
                        new SelectorItem<string> {Title = "Wooden", Item = "Wooden"},
                        new SelectorItem<string> {Title = "Inflatable", Item = "Inflatable"},
                        new SelectorItem<string> {Title = "Steel", Item = "Steel"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedPriceFrom;
        public SelectorItem<string> SelectedPriceFrom
        {
            get
            {
                return _selectedPriceFrom ?? (_selectedPriceFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPriceFrom = value;
                NotifyPropertyChanged("PriceFromName");
                Criteria.MinPrice = _selectedPriceFrom.Item != null ? Convert.ToInt32(_selectedPriceFrom.Item) : 0;
            }
        }
        public string PriceFromName
        {
            get { return SelectedPriceFrom.Title; }
            set { SelectedPriceFrom.Title = value; }
        }

        private SelectorItem<string> _selectedPriceTo;
        public SelectorItem<string> SelectedPriceTo
        {
            get
            {
                return _selectedPriceTo ?? (_selectedPriceTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPriceTo = value;
                NotifyPropertyChanged("PriceToName");
                Criteria.MaxPrice = _selectedPriceTo != null ? Convert.ToInt32(_selectedPriceTo.Item) : 0;
            }
        }
        public string PriceToName
        {
            get { return SelectedPriceTo.Title; }
            set { SelectedPriceTo.Title = value; }
        }

        public List<SelectorItem<string>> PriceItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="500", Title = "$500"},
                        new SelectorItem<string> {Item="1000", Title = "$1k"},
                        new SelectorItem<string> {Item="3000", Title = "$3k"},
                        new SelectorItem<string> {Item="5000", Title = "$5k"},
                        new SelectorItem<string> {Item="10000", Title = "$10k"},
                        new SelectorItem<string> {Item="15000", Title = "$15k"},
                        new SelectorItem<string> {Item="20000", Title = "$20k"},
                        new SelectorItem<string> {Item="30000", Title = "$30k"},
                        new SelectorItem<string> {Item="40000", Title = "$40k"},
                        new SelectorItem<string> {Item="60000", Title = "$60k"},
                        new SelectorItem<string> {Item="80000", Title = "$80k"},
                        new SelectorItem<string> {Item="100000", Title = "$100k"},
                        new SelectorItem<string> {Item="200000", Title = "$200k+"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedLengthFrom;
        public SelectorItem<string> SelectedLengthFrom
        {
            get
            {
                return _selectedLengthFrom ?? (_selectedLengthFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedLengthFrom = value;
                NotifyPropertyChanged("LengthFromName");
                Criteria.MinPrice = _selectedLengthFrom.Item != null ? Convert.ToInt32(_selectedLengthFrom.Item) : 0;
            }
        }
        public string LengthFromName
        {
            get { return SelectedLengthFrom.Title; }
            set { SelectedLengthFrom.Title = value; }
        }

        private SelectorItem<string> _selectedLengthTo;
        public SelectorItem<string> SelectedLengthTo
        {
            get
            {
                return _selectedLengthTo ?? (_selectedLengthTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedLengthTo = value;
                NotifyPropertyChanged("LengthToName");
                Criteria.MaxLength = _selectedLengthTo != null ? Convert.ToInt32(_selectedLengthTo.Item) : 0;
            }
        }
        public string LengthToName
        {
            get { return SelectedLengthTo.Title; }
            set { SelectedLengthTo.Title = value; }
        }

        public List<SelectorItem<string>> LengthItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="3", Title = "3m"},
                        new SelectorItem<string> {Item="5", Title = "5m"},
                        new SelectorItem<string> {Item="7", Title = "7m"},
                        new SelectorItem<string> {Item="9", Title = "9m"},
                        new SelectorItem<string> {Item="11", Title = "11m"},
                        new SelectorItem<string> {Item="13", Title = "13m"},
                        new SelectorItem<string> {Item="15", Title = "15m+"},
                    };
                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////


        public void UpdateFields()
        {
            NotifyPropertyChanged("Criteria");

            if (Criteria.Region != 0)
                SelectedRegion = App.TradeMe.Catalog.Areas.FirstOrDefault(l => l.LocalityId == Criteria.Region);

            if (!string.IsNullOrEmpty(Criteria.SortOrder))
                SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);

            if (!string.IsNullOrEmpty(Criteria.Type))
                SelectedType = TypeItems.FirstOrDefault(t => t.Item == Criteria.Type);

            if (Criteria.MinLength > 0)
                SelectedLengthFrom = LengthItems.FirstOrDefault(es => es.Item == Criteria.MinLength.ToString());
            if (Criteria.MaxLength > 0)
                SelectedLengthTo = LengthItems.FirstOrDefault(es => es.Item == Criteria.MaxLength.ToString());

            if (Criteria.MinPrice > 0)
            {
                string price = ((int)Criteria.MinPrice).ToString();
                SelectedPriceFrom = PriceItems.FirstOrDefault(p => p.Item == price);
            }
            if (Criteria.MaxPrice > 0)
            {
                string price = ((int)Criteria.MaxPrice).ToString();
                SelectedPriceTo = PriceItems.FirstOrDefault(p => p.Item == price);
            }
        }

        public void Reset()
        {
            SelectedRegion = null;
            SelectedSortBy = null;
            SelectedType = null;
            SelectedPriceFrom = null;
            SelectedPriceTo = null;
            SelectedLengthFrom = null;
            SelectedLengthTo = null;
            UpdateFields();
        }
    }
}
